var skill = {
	hasCardFactoryById: function(card_factory_id) {
		var card_factory_list = this._getCardFactoryIdList();
		return card_factory_list.indexOf(card_factory_id) !== -1;
	},
	_getCardFactoryIdList: function() {
		var card_factory_list = this.card_factory_list;
		if (!Array.isArray(card_factory_list)) {
			card_factory_list = this.card_factory_list = [];
			this.save();
		}
		return card_factory_list;
	},
	_getCardFactory: function(card_factory_id) {
		var card_factory_list = this._getCardFactoryIdList();
		if (card_factory_list.indexOf(card_factory_id) === -1) {
			throwCusError("会员卡不属于指定商家")
		}
		var card_factory = CardFactoryCon.get(card_factory_id);
		if (!card_factory) {
			throwCusError("找不到指定会员卡")
		}
		return card_factory;
	},
	getCardFactoryList: function(num, page) {
		var card_factory_list = this._getCardFactoryIdList();
		card_factory_list = data_cut.page(card_factory_list, num, page);
		card_factory_list = card_factory_list.map(function(card_factory_id) {
			return CardFactoryCon.get(card_factory_id);
		});
		return card_factory_list;
	},
	addCardFactory: function(card_factory_info) {
		var card_factory_list = this._getCardFactoryIdList();
		if (card_factory_list.length >= config.busCardFactoryNumber) {
			throwCusError("商家同时的拥有会员卡数量已经达到上限")
		}
		card_factory_info.bus_id = this._id;
		var card_factory = new CardFactoryCon(card_factory_info);
		var card_factory_id = card_factory._id
		card_factory_list.unshift(card_factory_id);
		this.save();
		return card_factory;
	},
	updateCardFactory: function(card_factory_id, card_factory_info) {
		var card_factory = this._getCardFactory(card_factory_id);
		card_factory.setInfo(card_factory_info);
		return card_factory;
	},
	removeCardFactory: function(card_factory_id) {
		var card_factory = this._getCardFactory(card_factory_id);
		this.card_factory_list = $Array.remove(this.card_factory_list, card_factory._id);
		card_factory.destroy();
		this.save();
	},
	//商品操作
	getGoodsListFromCardFactory_PageInfo: function(card_factory_id, num, page, options) {
		var card_factory = this._getCardFactory(card_factory_id);
		return card_factory.getGoodsList_PageInfo(num, page, options);
	},
	addGoodsToCardFactory: function(card_factory_id, goods_info, num) {
		var card_factory = this._getCardFactory(card_factory_id);
		var goods = new GoodsCon(goods_info, this._id, num || 0);
		card_factory.addGoods(goods._id);
		return goods;
	},
	updateGoodsFromCardFactory: function(card_factory_id, goods_id, goods_info) {
		var card_factory = this._getCardFactory(card_factory_id);
		return card_factory.updateGoods(goods_id, goods_info);
	},
	updateGoodsIndexFromCardFactory: function(card_factory_id, goods_id, index) {
		var card_factory = this._getCardFactory(card_factory_id);
		return card_factory.updateGoodsIndex(goods_id, index);
	},
	removeGoodsFromCardFactory: function(card_factory_id, goods_id) {
		var card_factory = this._getCardFactory(card_factory_id);
		return card_factory.removeGoods(goods_id);
	},
};
module.exports = skill;
var db = require("../db/index");
var CardFactoryCon = require("../card_factory/_con");
var DomainCon = require("../domain/_con");
var GoodsCon = require("../goods/_con");
var UserCon = require("../../core/user/_con");
var BusCon = require("../../core/bus/_con");
var AchCon = require("../ach/_con");
var ClassifyCon = require("../bus.classify/_con");
var TagCon = require("../bus.tag/_con");
var LeagueCon = require("../league/_con");
var BuffCon = require("../buff/_con");
var BusUserBindCon = require("../bus_user_bind/_con");
var BusGuideCon = require("../bus.guide/_con");
var CommentCon = require("../comment/_con");
var config = require("../_include/config");
var data_cut = require("../_include/data_cut");
var helper = require("../_include/helper");
var md5 = require("../_include/md5");
var $Object = require("../_include/$.Object");
var $Array = require("../_include/$.Array");
var tld = require('tldjs');
var fs = require("fs");
var Fiber = require('fibers');